package com.mes.handler.quality;

import com.mes.data.EmesProcess;
import com.mes.quality.data.*;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.hibernate.Query;
import org.openbravo.client.kernel.BaseActionHandler;
import org.openbravo.dal.service.OBDal;
import org.openbravo.database.SessionInfo;
import org.openbravo.model.ad.access.User;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public class EmesqQualityDataCollectionHandler extends BaseActionHandler{
	private OBDal obdal = OBDal.getInstance();
	
	@Override
	protected JSONObject execute(Map<String, Object> parameters,String content) {
		JSONObject result = new JSONObject();
		try {
			JSONObject request = new JSONObject(content);
			String method = request.getString("method");
			
			result = (JSONObject) this.getClass().getMethod(method, JSONObject.class).invoke(this,request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	//加载工序信息
	public JSONObject loadProcessDataInfo(final JSONObject requestData) throws JSONException{
		JSONObject result = new JSONObject();
		String hql = "select new map(ep.id as emesProcessId,ep.name as emesProcessName) from Emes_Process ep order by ep.routeNo asc";
		List list = obdal.getSession().createQuery(hql).list();
		User user = obdal.get(User.class, SessionInfo.getUserId());
		JSONArray arr = new JSONArray();
		if(null == list || list.size() == 0){
			result.put("result", false);
			result.put("msg", "没有工序数据！");
		}else{
			for(int i=0;i<list.size();i++){
				Map map = (Map)list.get(i);
				arr.put(map);
			}
			result.put("result", true);
			result.put("msg", "加载工序数据成功！");
		}
		result.put("dataInfo",arr);
		EmesProcess emesProcess = user.getDefaultProcess();
		String defaultValue = null == emesProcess ? "" : emesProcess.getName();
		result.put("defaultValue", defaultValue);
		result.put("userName", user.getName());
		return result;
	}
	
	//获取工序的数据采集项
	public JSONObject getQualityItem(final JSONObject requestData) throws JSONException {
		JSONObject responseResult = new JSONObject();
		String emesProcessId = requestData.getString("emesProcessId");
		EmesProcess emesProcess = obdal.get(EmesProcess.class, emesProcessId);
		//验证产品是否有检验要求
		String hql = "from " + EmesqQualityRouteRelation.ENTITY_NAME + " rr where rr.product.searchKey= '0201030201001' and rr.emesProcess.emesProcessOpr=?";
		Query query = obdal.getSession().createQuery(hql);
		query.setEntity(0, emesProcess.getEmesProcessOpr());
		EmesqQualityRouteRelation routeRelation = (EmesqQualityRouteRelation)query.uniqueResult();
		
		List<EmesqInspectionGroupItem> list = null;
		
		if (routeRelation == null) {
			//验证工序是否有检验要求
			hql = "from " + EmesqQualityROR.ENTITY_NAME + " pr where pr.emesProcessOpr = ?";
			query =obdal.getSession().createQuery(hql);
			query.setEntity(0, emesProcess.getEmesProcessOpr());
			EmesqQualityROR ror = (EmesqQualityROR)query.uniqueResult();
			
			if (ror == null) {
				responseResult.put("text", "该工序检验项！");
				responseResult.put("result", false);
				
				return responseResult;
			} else {
				list = ror.getEmesqInspectionGroup().getEmesqInspectionGroupItemList();
			}
		} else {
			list = routeRelation.getEmesqInspectionGroup().getEmesqInspectionGroupItemList();
		}
		List result = new ArrayList();
		
		JSONObject map = null;
		EmesqInspectionItem qItem = null;
		for (EmesqInspectionGroupItem item : list) {
			qItem = item.getEmesqInspectionItem();
			map = new JSONObject();
			map.put("name", qItem.getName());
			map.put("type", qItem.getDataType());
			map.put("id", qItem.getId());
			map.put("groupId", item.getEmesqInspectionGroup().getId());
			
			if ("SELECTOR".equals(qItem.getDataType()) && qItem.getEmesInspectioEnumData() != null) {// 选项
				String title = qItem.getEmesInspectioEnumData().getName();
				map.put("title", title);
				map.put("child", new JSONObject());
				for (EmesqInspectionEDItem dItem : qItem.getEmesInspectioEnumData().getEmesqInspectionEDItemList()) {
					map.getJSONObject("child").put(dItem.getId(), dItem.getResult());
				}
			}
			result.add(map);
		}
		
		responseResult.put("list", result);
		responseResult.put("result", true);
		return responseResult;
	}
}
